home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / Scrawl / Main.cs next >
Encoding:
Text File  |  2004-09-27  |  11.4 KB  |  328 lines

  1. using System;
  2. using System.Drawing;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using Microsoft.DirectX;
  6. using Microsoft.DirectX.DirectInput;
  7. using Microsoft.Samples.DirectX.UtilityToolkit;
  8. using System.Threading;
  9. using System.Diagnostics;
  10. using System.IO;
  11.  
  12. namespace Scrawl
  13. {
  14.     /// <summary>
  15.     /// Summary description for frmMain.
  16.     /// </summary>
  17.     public class frmMain : System.Windows.Forms.Form
  18.     {
  19.         /// <summary>
  20.         /// Required designer variable.
  21.         /// </summary>
  22.         private System.ComponentModel.Container components = null;
  23.         
  24.         private AutoResetEvent DataArrivalEvent = null;
  25.         private Graphics ApplicationGraphics = null;
  26.         private ContextMenu ApplicationMenu = null;
  27.         private const int SampleBufferSize = 16;
  28.         private Device ApplicationDevice = null;
  29.         private Point CurrentPoint = new Point(); // Virtual coordinates
  30.         private Point OldPoint = new Point();
  31.         private Thread DeviceThread = null;
  32.         private Cursor CursorBlank = null;
  33.         private bool Drawing = false;
  34.         private int Sensitivity; // Mouse sensitivity 
  35.         private int dxFuzz; // Leftover x-fuzz from scaling 
  36.         private int dyFuzz; // Leftover y-fuzz from scaling 
  37.         private const int ScawlCXBitmap = 512;
  38.         private const int ScrawlCYBitmap = 300;
  39.  
  40.         public frmMain()
  41.         {
  42.             //
  43.             // Required for Windows Form Designer support
  44.             //
  45.             InitializeComponent();
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Clean up any resources being used.
  50.         /// </summary>
  51.         protected override void Dispose( bool disposing )
  52.         {
  53.             if( disposing )
  54.             {
  55.                 if (components != null) 
  56.                 {
  57.                     components.Dispose();
  58.                 }
  59.             }
  60.             base.Dispose( disposing );
  61.         
  62.             DataArrivalEvent.Set();
  63.         }
  64.  
  65.         #region Windows Form Designer generated code
  66.         /// <summary>
  67.         /// Required method for Designer support - do not modify
  68.         /// the contents of this method with the code editor.
  69.         /// </summary>
  70.         private void InitializeComponent()
  71.         {
  72.             // 
  73.             // frmMain
  74.             // 
  75.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  76.             this.BackColor = System.Drawing.Color.White;
  77.             this.ClientSize = new System.Drawing.Size(512, 294);
  78.             this.Cursor = System.Windows.Forms.Cursors.Cross;
  79.             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  80.             this.MaximizeBox = false;
  81.             this.Name = "frmMain";
  82.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  83.             this.Text = "Scrawl";
  84.             this.Load += new System.EventHandler(this.frmMain_Load);
  85.             this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.frmMain_KeyUp);
  86.             this.Activated += new System.EventHandler(this.frmMain_Activated);
  87.         }
  88.         #endregion
  89.  
  90.         /// <summary>
  91.         /// The main entry point for the application.
  92.         /// </summary>
  93.         [STAThread]
  94.         static void Main() 
  95.         {
  96.             Application.Run(new frmMain());
  97.         }
  98.  
  99.         private void DataArrivalHandler()
  100.         {
  101.             while (Created)
  102.             {                
  103.                 DataArrivalEvent.WaitOne();
  104.              
  105.                 if(!Created)
  106.                     break;
  107.  
  108.                 BufferedDataCollection col = null;
  109.  
  110.                 try
  111.                 {
  112.                    col = ApplicationDevice.GetBufferedData();
  113.                 }
  114.                 catch(InputException e)
  115.                 {
  116.                     if(e is InputLostException || e is AcquiredException)
  117.                     {
  118.                         SetAcquire(true);
  119.                         continue;
  120.                     }
  121.                 }
  122.                 
  123.                 if(null == col)
  124.                     continue;
  125.  
  126.                 foreach(BufferedData data in col)
  127.                 {
  128.                     switch(data.Offset)
  129.                     {
  130.                         case (int)MouseOffset.X:
  131.                             UpdateCursorPosition(data.Data, 0);
  132.                             break;
  133.                         case (int)MouseOffset.Y:
  134.                             UpdateCursorPosition(0, data.Data);
  135.                             break;
  136.                         case (int)MouseOffset.Button0:
  137.                             if( 0 != (data.Data & 0x80) )
  138.                             {
  139.                                 Cursor = CursorBlank;
  140.                                 OldPoint = CurrentPoint = PointToClient(Cursor.Position);
  141.                                 Drawing = true;
  142.                             }
  143.                             else
  144.                             {
  145.                                 Cursor = Cursors.Cross;
  146.                                 Cursor.Position = PointToScreen(CurrentPoint);
  147.                                 Drawing = false;
  148.                             }
  149.                             break;
  150.                         case (int)MouseOffset.Button1:
  151.                             SetAcquire(false);
  152.                             ApplicationMenu.Show(this, new Point(10,10));
  153.                             break;
  154.                     }
  155.                 }
  156.                 if(Drawing)
  157.                 {                    
  158.                     ApplicationGraphics.DrawLine(new Pen(Color.Red), new Point(CurrentPoint.X, CurrentPoint.Y), OldPoint);
  159.                     OldPoint = CurrentPoint;
  160.                 }
  161.             }
  162.         }
  163.  
  164.         private void UpdateCursorPosition(int dx, int dy)
  165.         {   
  166.  
  167.             //-----------------------------------------------------------------------------
  168.             // Name: UpdateCursorPosition()
  169.             // Desc: Move our private cursor in the requested direction, subject
  170.             //       to clipping, scaling, and all that other stuff.
  171.             //
  172.             //       This does not redraw the cursor.  You need to do that yourself.
  173.             //-----------------------------------------------------------------------------
  174.  
  175.             // Pick up any leftover fuzz from last time.  This is important
  176.             // when scaling down mouse motions.  Otherwise, the user can
  177.             // drag to the right extremely slow for the length of the table
  178.             // and not get anywhere.
  179.             
  180.             dx += dxFuzz;     
  181.             dxFuzz = 0;
  182.  
  183.             dy += dyFuzz;
  184.             dyFuzz = 0;            
  185.  
  186.             switch(Sensitivity) 
  187.             {
  188.                 case 1:     // High sensitivity: Magnify! 
  189.                     dx *= 2;
  190.                     dy *= 2;
  191.                     break;
  192.  
  193.                 case -1:    // Low sensitivity: Scale down 
  194.                     dxFuzz = dx % 2;  // remember the fuzz for next time 
  195.                     dyFuzz = dy % 2;
  196.                     dx /= 2;
  197.                     dy /= 2;
  198.                     break;
  199.  
  200.                 case 0:     // normal sensitivity 
  201.                     // No adjustments needed 
  202.                     break;
  203.             }
  204.  
  205.             CurrentPoint.X += dx;
  206.             CurrentPoint.Y += dy;
  207.  
  208.             // clip the cursor to our client area
  209.             if( CurrentPoint.X < 0 ) 
  210.                 CurrentPoint.X = 0;
  211.  
  212.             if( CurrentPoint.X >= ScawlCXBitmap )
  213.                 CurrentPoint.X = ScawlCXBitmap - 1;
  214.  
  215.             if( CurrentPoint.Y < 0 )  
  216.                 CurrentPoint.Y = 0;
  217.  
  218.             if( CurrentPoint.Y >= ScrawlCYBitmap ) 
  219.                 CurrentPoint.Y = ScrawlCYBitmap - 1;
  220.         }
  221.  
  222.         private void frmMain_Load(object sender, System.EventArgs e)
  223.         {
  224.             ApplicationDevice = new Device(SystemGuid.Mouse);
  225.             ApplicationDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Foreground | CooperativeLevelFlags.NonExclusive);
  226.  
  227.             DataArrivalEvent = new AutoResetEvent(false);
  228.             DeviceThread = new Thread(new ThreadStart(DataArrivalHandler));
  229.             DeviceThread.Start();
  230.  
  231.             ApplicationDevice.SetEventNotification(DataArrivalEvent);
  232.             ApplicationDevice.Properties.BufferSize = SampleBufferSize;
  233.             SetAcquire(true);
  234.             ApplicationGraphics = this.CreateGraphics();
  235.             
  236.             MenuItem[] PopoutItem = new MenuItem[3];
  237.             PopoutItem[0] = new MenuItem("&Low\t1", new EventHandler(ContextSubMenuEvent));
  238.             PopoutItem[1] = new MenuItem("&Normal\t2", new EventHandler(ContextSubMenuEvent));
  239.             PopoutItem[2] = new MenuItem("&High\t3", new EventHandler(ContextSubMenuEvent));
  240.             PopoutItem[1].Checked = true;
  241.             
  242.             MenuItem[] item = new MenuItem[3];
  243.             item[0] = new MenuItem("&Exit", new EventHandler(MenuEventExit));
  244.             item[1] = new MenuItem("&Clear", new EventHandler(MenuEventClear));
  245.             item[2] = new MenuItem("&Sensitivity");
  246.             item[2].MenuItems.AddRange(PopoutItem);
  247.  
  248.             ApplicationMenu = new ContextMenu(item);
  249.             ApplicationMenu.Popup += new EventHandler(MenuCreated);
  250.             this.ContextMenu = ApplicationMenu;
  251.             string cursorFile = Utility.FindMediaFile("Misc\\blank.cur");
  252.  
  253.             CursorBlank = new Cursor(cursorFile);
  254.             Point p = new Point(this.ClientRectangle.Bottom / 2, this.ClientRectangle.Right / 2);
  255.             Cursor.Position = PointToScreen(p);
  256.             OldPoint = CurrentPoint = p;
  257.         }
  258.         private void MenuCreated(object sender, System.EventArgs e)
  259.         {
  260.             SetAcquire(false);
  261.         }
  262.         private void MenuEventExit(object sender, System.EventArgs e)
  263.         {
  264.             SetAcquire(false);
  265.             Close();
  266.         }
  267.         private void MenuEventClear(object sender, System.EventArgs e)
  268.         {
  269.             OnClear();
  270.             SetAcquire(true);
  271.         }
  272.         private void ContextSubMenuEvent(object sender, System.EventArgs e)
  273.         {
  274.             MenuItem item = (MenuItem)sender;
  275.             int i = -1;
  276.  
  277.             foreach(MenuItem m in ApplicationMenu.MenuItems[2].MenuItems)
  278.             {
  279.                 if ( m.Equals(item) )
  280.                 {
  281.                     m.Checked = true;
  282.                     Sensitivity = i;
  283.                 }
  284.                 else
  285.                     m.Checked = false;
  286.                 i++;
  287.             }
  288.             item.Checked = true;
  289.  
  290.             SetAcquire(true);
  291.         }
  292.         private void frmMain_Activated(object sender, System.EventArgs e)
  293.         {
  294.             SetAcquire(true);
  295.         }
  296.         private void SetAcquire(bool state)
  297.         {
  298.             if (null != ApplicationDevice)
  299.             {
  300.                 if(state)
  301.                 {
  302.                     try{ApplicationDevice.Acquire();}
  303.                     catch(InputException){}
  304.                 }
  305.                 else
  306.                 {
  307.                     try{ApplicationDevice.Unacquire();}
  308.                     catch(InputException){}
  309.                 }
  310.             }
  311.         }
  312.         private void OnClear()
  313.         {
  314.             //-----------------------------------------------------------------------------
  315.             // Name: OnClear()
  316.             // Desc: Makes the form white
  317.             //-----------------------------------------------------------------------------            
  318.             ApplicationGraphics.Clear(Color.White);            
  319.         }
  320.  
  321.         private void frmMain_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  322.         {
  323.             if(e.KeyCode == Keys.Escape)
  324.                 Close();
  325.         }
  326.  
  327.     }
  328. }